﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class CategoryDomainService : ICategoryDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, Category> _repository;

        public CategoryDomainService(IRepository<Guid, Category> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public Category Create(Category category)
        {
            return _repository.Create(category);
        }

        public Category Get(Guid id, string include="")
        {
            var category = _repository.Get(id);
            if(category!=null
                && !string.IsNullOrEmpty(include)
                && include.Split(',').Contains("intents"))
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                var intents = intentDomainService.GetByCategoryId(id, include);
                category.Intents = intents;
            }
            return category;
        }

        public Category Update(Guid id, CategoryUpdateBo bo)
        {
            Category category = _repository.Get(bo.Id);

            category.Id = bo.Id;
            category.ParentId = bo.ParentId;
            category.Name = bo.Name;

            _repository.Update(category);

            return category;

        }

        public IEnumerable<Category> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            Category category = Get(id,"intents");
            if(category!=null
                && category.Intents != null)
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                foreach (var intent in category.Intents)
                {
                    intentDomainService.Delete(intent.Id);
                }
            }
            _repository.Delete(category);

        }

        public List<Category> GetByBotId(Guid botId)
        {
            return _repository.List(new CategoryFilterSpecification(botId)).ToList();
        }

        public void Reassign(Guid id, Guid targetCategoryId)
        {
            Category category = _repository.Get(id);
            category.ParentId = targetCategoryId;

            var childs = _repository.List(new CategoryFilterSpecification(category.BotId, id));
            foreach(var child in childs)
            {
                child.ParentId = targetCategoryId;
                _repository.Update(child);
            }

            _repository.Delete(category);
        }
        public Guid CreateCategory(Guid botId, string category)
        {
            //返回最后一个新增cagegory Id 值

            Guid categoryId = Guid.Empty;

            if (string.IsNullOrEmpty(category))
            {
                return Guid.Empty;
            }

            var rootCatagoryDto = GetRootCatagory(botId);
            if (rootCatagoryDto != null && rootCatagoryDto.Id != Guid.Empty)
            {
                categoryId = rootCatagoryDto.Id;
            }
            else
            {
                rootCatagoryDto = Create(new Category
                {
                    Id = Guid.NewGuid(),
                    BotId = botId,
                    ParentId = null,
                    Name = "/",
                    IsDeleted = false,
                });
                categoryId = rootCatagoryDto.Id;
            }

            Guid parentId = rootCatagoryDto.Id;
            string[] categoryArr = category.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < categoryArr.Length; i++)
            {
                string categoryName = categoryArr[i];
                var botCatagory = GetCatagory(botId, parentId, categoryName);
                if (botCatagory != null)
                {

                }
                else
                {
                    //创建
                    var newBotCatagory = new Category
                    {
                        Id = Guid.NewGuid(),
                        BotId = botId,
                        ParentId = parentId,
                        Name = categoryName,
                        IsDeleted = false,
                    };
                    botCatagory = Create(newBotCatagory);
                    if (botCatagory != null && botCatagory.Id != Guid.Empty)
                    {
                        
                    }
                    else
                    {
                        throw new Exception(string.Format("Create category fail"));
                    }
                }

                categoryId = botCatagory.Id;
                parentId = botCatagory.Id;
            }

            return categoryId;
        }
        public Category GetRootCatagory(Guid botId)
        {
            var categorys = _repository.List(new CategoryFilterSpecification(botId));
            return categorys.FirstOrDefault(a => a.BotId == botId && a.ParentId == null && a.IsDeleted == false);
        }
        public Category GetCatagory(Guid botId, Guid parentId, string categoryName)
        {
            var categorys = _repository.List(new CategoryFilterSpecification(botId,parentId,categoryName));
            return categorys.FirstOrDefault();
        }
        public Category GetCatagoriesByBotIdAndName(Guid botId, string categoryName)
        {
            var categorys = _repository.List(new CategoryFilterSpecification(botId, categoryName));
            return categorys.FirstOrDefault();
        }
    }
}
